//
//  TCpdfView.m
//  EFBTerminalCharts
//
//  Created by xiu-imac on 13-11-29.
//  Copyright (c) 2013年 adcc. All rights reserved.
//

#import "TCpdfView.h"
#import "TCAirportModel.h"
#import "ChartFileHelper.h"
#import "ChartContext.h"
#import "EFBNAIP.h"
@interface TCpdfView()


@end

@implementation TCpdfView

-(void)dealloc
{
	if (_reader) {
		[_reader saveBeforeReleaseOrHiddenReaderView];
	}
	[_reader release];
	[super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
		[self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

-(void)showPDFWithPath:(NSMutableDictionary *)chartDic
{
    if (self.reader) {
        BOOL isChart = [[chartDic objectForKey:@"isChart"] isEqualToString:@"YES"]?YES:NO;
        if (!isChart) {
            [self saveReaderViewPlistBefroeRelease];
        }
        [self.reader.view removeFromSuperview];
        self.reader=nil;
        self.reader.delegate = nil;
    }

    NSString *uuid=self.chartID;
    NSString *revision=[[ChartContext instance] objectForKey:@"ChartVersion"];
    
    // 蓝牙传输新加内容
    [[NSUserDefaults standardUserDefaults] setValue:uuid forKey:@"blueUUID"];
    //-------  航图路径  此处做 是否为打开 co 或者是 AR 航图文件 (存储路劲与航图路劲不相同, 而且为可变信息)
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *childClass = [userDefaults objectForKey:@"chartChildSelected"];
    NSString *pdfPath = [[[ChartFileHelper pathForCharts:uuid] stringByDeletingPathExtension] stringByAppendingPathComponent:[NSString stringWithFormat:@"data/%@",[chartDic objectForKey:@"file_name"]]];
    if ([childClass isEqualToString:@"CO"]) {
        pdfPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/companyCharts/%@", [chartDic objectForKey:@"file_name"]]];
    }else if([childClass isEqualToString:@"AR"]){
        pdfPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/rnparCharts/%@", [chartDic objectForKey:@"file_name"]]];
    }
//    else{
//        pdfPath = [[[ChartFileHelper pathForCharts:uuid] stringByDeletingPathExtension] stringByAppendingPathComponent:[NSString stringWithFormat:@"data/%@",[chartDic objectForKey:@"file_name"]]];
//    }
    
    ReaderDocument *document = nil;
    if ([childClass isEqualToString:@"CO"] || [childClass isEqualToString:@"AR"]) {
        document =[ReaderDocument newWithDocumentFilePath:pdfPath password:nil];
    }else{
        NSString *passWord = [EFBNAIP passwordWithFile:[pdfPath lastPathComponent] andVersion:revision];
        document =[ReaderDocument newWithDocumentFilePath:pdfPath password:passWord];
    }

    if (document)
    {
        self.reader=[[[ReaderViewController alloc] initWithReaderDocument:document] autorelease];
		self.reader.isChar = [[chartDic objectForKey:@"isChart"] isEqualToString:@"YES"]?YES:NO;
        self.reader.isFullScreen=_isFull;//标记当前航图界面是否全屏
        self.reader.isPerformance=NO;
        self.reader.char_rotation=[[[TCAirportModel getRotateDic]objectForKey:[chartDic objectForKey:@"file_name"]] intValue];
        self.reader.char_PDFName=[chartDic objectForKey:@"file_name"];
        self.reader.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
		[self.reader.view setBackgroundColor:[UIColor clearColor]];
		self.reader.delegate = self;
        if ([childClass isEqualToString:@"黄页"])
        {
            self.reader.isCo = YES;
        }
		[self addSubview:self.reader.view];
        [document release];
	}
}

-(void)saveReaderViewPlistBefroeRelease
{
    if (self.reader) {
        [self.reader saveBeforeReleaseOrHiddenReaderView];
    }
}

#pragma mark - ReaderViewController Delegate Method

- (void)dismissReaderViewController:(ReaderViewController *)viewController{
    return;
}

- (void)contentViewTapGestureRecognizer:(BOOL)isHidden{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"pdfTapped" object:[NSNumber numberWithBool:isHidden]];
    
}

- (void)updatePDFPageNum:(NSInteger)pageIndex
{
	
}

- (void)clearReaderBeforeRelease
{
    if (self.reader) {
        [self saveReaderViewPlistBefroeRelease];
        [self.reader.view removeFromSuperview];
        self.reader=nil;
//        self.reader.delegate = nil;
    }
}
@end
